<!-- 5. 设置下拉框中的默认随机选中项，自己书写结构和js -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文档标题</title>
</head>
<body>
<button id="choose">随机选择</button>
<select id="heros">
    <option>王昭君</option>
    <option>李白</option>
    <option>后羿</option>
    <option>凯皇</option>
    <option>诸葛亮</option>
    <option>黄忠</option>
    <option>貂蝉</option>
    <option>吕布</option>
    <option>鲁班七号</option>
</select>
<script>
  function chooseOne() {
    const heros = document.getElementById("heros").children;
    for (const hero of heros) {
      hero.selected = false;
    }
    const randomIndex = Math.floor(Math.random() * heros.length);
    heros[randomIndex].selected = true;
  }

  window.onload = () => chooseOne();

  document.getElementById("choose").onclick = () => chooseOne();

</script>
</body>
</html>
